package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.SpUsualOutCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpUsualOutResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpUsualOutUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpUsualOutEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpUsualOutConvert {
    SpUsualOutConvert INSTANCE = Mappers.getMapper(SpUsualOutConvert.class);

    SpUsualOutResultDTO convertTo(SpUsualOutEntity s);

    SpUsualOutEntity createTo(SpUsualOutCreateDTO s);

    SpUsualOutEntity updateTo(SpUsualOutUpdateDTO s);

    default SpUsualOutEntity create(SpUsualOutCreateDTO s, Long tenantId, NormalAuditActionStatusEnum action) {
        SpUsualOutEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setPreAction(action);
        additionalInformation(entity, s.getDestinationList());
        return entity;
    }

    default SpUsualOutEntity update(SpUsualOutUpdateDTO s) {
        SpUsualOutEntity entity = updateTo(s);
        additionalInformation(entity, s.getDestinationList());
        return entity;
    }

    default SpUsualOutResultDTO convert(SpUsualOutEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }

        SpUsualOutResultDTO result = convertTo(s);

        List<String> destinationList = new ArrayList<>();
        destinationList.add(s.getQwdszs());
        destinationList.add(s.getQwdszd());
        Actions.accept(s.getQwdszx(), v -> destinationList.add(s.getQwdszx()));
        Actions.accept(s.getQwdxz(), v -> destinationList.add(s.getQwdxz()));
        result.setDestinationList(destinationList);
        return result;
    }

    default void additionalInformation(SpUsualOutEntity t, List<String> destinationList) {
        if (Objects.isNull(t) || CollectionUtils.isEmpty(destinationList)) {
            return;
        }

        t.setQwdszs(destinationList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        t.setQwdszd(destinationList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));
        Actions.apply(destinationList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH, () -> t.setQwdszx(destinationList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX)));
        Actions.apply(destinationList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH, () -> t.setQwdxz(destinationList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX)));
    }

}
